package laborhazi;

import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JFrame;
import java.awt.Color;
import java.awt.BorderLayout;
import java.awt.Graphics;
//import javax.swing.ImageIcon;
import javax.swing.WindowConstants;
import java.awt.Font;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/*
 * import java.awt.event.MouseAdapter;
 * import java.awt.event.MouseEvent;
 * import java.awt.event.MouseListener;
 * import java.awt.font.*;
 * import java.awt.GridLayout;
 * import java.awt.Graphics;
*/
public class Viewer extends JFrame{
	
	/**
	 * Mandatory serial version UID
	 */
	private static final long serialVersionUID = 1L;
	private final int WIDTH = 540, HEIGHT = 540;
	private int size = 6;
	private int phase = 0;
	private boolean[][] blown;
/*
	private  int size = 30;
	*/
	private Field field;
	private LemmingAnim lemmingAnim;
	private StoneAnim stoneAnim;
	private FinishAnim finishAnim;
	private StartAnim startAnim;
	private BlowAnim blowAnim;
	private MouseControl mouseControl;
	private JPanel buttonComponent = null;
	// private JPanel mapComponent = null;
	private JPanel mapComponent = null;
	private JButton barricadeButton = null;
	private JButton erectorButton = null;
	private JButton drillButton = null;
	private JButton parachuteButton = null;
	private JButton explosiveButton = null;
	private JButton giveupButton = null;
	private String label = null;
	private int toolNum = 0;
	private Animation[] animationList = null;
	private Thread viewThread;
	public Game game = null;
	private Graphics g;
	// private ImageIcon im1,im2,im3,im4,im5,im6;
	private Dimension butcompSize, buttonSize,mapcompSize;
	
	private void updateButtons() {
		if (game.toolbar != null) {
		toolNum = game.toolbar.getBarricade();
		label = new String("barricade(" + toolNum + ")");
		barricadeButton.setText(label);
		toolNum = game.toolbar.getParachute();
		label = new String("parachute(" + toolNum + ")");
		parachuteButton.setText(label);
		toolNum = game.toolbar.getDrill();
		label = new String("drill(" + toolNum + ")");
		drillButton.setText(label);
		toolNum = game.toolbar.getErector();
		label = new String("erector(" + toolNum + ")");
		erectorButton.setText(label);
		toolNum = game.toolbar.getExplosive();
		label = new String("explosive(" + toolNum + ")");
		explosiveButton.setText(label);
		}
	}
	
	public Viewer(Game g, Field f) {
		super("Lemmings Game", null);
		game = g;
		field = f;
		size = f.getSize();
		f.setViewer(this);
		blown = new boolean[size+10][size+10];
		mouseControl = new MouseControl(this, f, g);
		stoneAnim = new StoneAnim(this);
		lemmingAnim = new LemmingAnim(this);
		finishAnim = new FinishAnim(this);
		startAnim = new StartAnim(this);
		blowAnim = new BlowAnim(this);
		/* default sizes */
		butcompSize = new Dimension(600,34);
		buttonSize = new Dimension(80,24);
		mapcompSize = new Dimension(600,600);
		
		/* setting the components */
		buttonComponent = new JPanel(true);
		//buttonComponent.setMaximumSize(butcompSize);
		buttonComponent.setMinimumSize(butcompSize);
		//buttonComponent.setPreferredSize(butcompSize);
		buttonComponent.setBackground(Color.WHITE);
		mapComponent = new JPanel(true);
		mapComponent.setMinimumSize(mapcompSize);
		mapComponent.setBackground(Color.WHITE);
		
		/* setting the buttons */
		
		Font myFont = new Font("Dialog", Font.PLAIN, 8);
		toolNum = 0;
		
		barricadeButton = new JButton();
		if (game.toolbar != null) 
			toolNum = game.toolbar.getBarricade();
		label = new String("barricade(" + toolNum + ")");
		barricadeButton.setText(label);
		barricadeButton.setFont(myFont);
		
		parachuteButton = new JButton();
		if (game.toolbar != null)
			toolNum = game.toolbar.getParachute();
		label = new String("parachute(" + toolNum + ")");
		parachuteButton.setText(label);
		parachuteButton.setFont(myFont);
		
		drillButton = new JButton();
		if (game.toolbar != null)
			toolNum = game.toolbar.getDrill();
		label = new String("drill(" + toolNum + ")");
		drillButton.setText(label);
		drillButton.setFont(myFont);
		
		erectorButton = new JButton();
		if (game.toolbar != null)
			toolNum = game.toolbar.getErector();
		label = new String("erector(" + toolNum + ")");
		erectorButton.setText(label);
		erectorButton.setFont(myFont);
		
		explosiveButton = new JButton();
		if (game.toolbar != null)
			toolNum = game.toolbar.getExplosive();
		label = new String("explosive(" + toolNum + ")");
		explosiveButton.setText(label);
		explosiveButton.setFont(myFont);
		
		giveupButton = new JButton("give up");
		giveupButton.setFont(myFont);
		/*
		im1 = new ImageIcon("barricade.gif");
		barricadeButton = new JButton(im1);
		barricadeButton.setMinimumSize(buttonSize);
		barricadeButton.setPreferredSize(buttonSize);
		parachuteButton = new JButton("parachute");
		//im2 = new ImageIcon("parachute.gif");
		parachuteButton.setMinimumSize(buttonSize);
		parachuteButton.setPreferredSize(buttonSize);
		im3 = new ImageIcon("drill.gif");
		drillButton = new JButton(im3);
		drillButton.setMinimumSize(buttonSize);
		drillButton.setMinimumSize(buttonSize);
		//erectorButton = new JButton("erector");
		im4 = new ImageIcon("erector.gif");
		erectorButton = new JButton(im4);
		erectorButton.setMinimumSize(buttonSize);
		erectorButton.setMinimumSize(buttonSize);
		//im5 = new ImageIcon("explosive.gif");
		explosiveButton = new JButton("explosive");
		explosiveButton.setMinimumSize(buttonSize);
		explosiveButton.setMinimumSize(buttonSize);
		im6 = new ImageIcon("giveup.gif");
		giveupButton = new JButton(im6);
		giveupButton.setMinimumSize(buttonSize);
		giveupButton.setMinimumSize(buttonSize);
		*/
		/* adding actionlisteners */
		giveupButton.addActionListener(new ActionListener(){
			public void actionPerformed( ActionEvent ae ){
				game.end();
				}
			}
		);
		parachuteButton.addActionListener(new ActionListener(){
			public void actionPerformed( ActionEvent ae ){
				if (game.toolbar != null)
					game.toolbar.highlight(new Parachute());
				}
			}
		);
		drillButton.addActionListener(new ActionListener(){
			public void actionPerformed( ActionEvent ae ){
				if (game.toolbar != null)
					game.toolbar.highlight(new Drill());
				}
			}
		);
		erectorButton.addActionListener(new ActionListener(){
			public void actionPerformed( ActionEvent ae ){
				if (game.toolbar != null)
					game.toolbar.highlight(new Erector());
				}
			}
		);
		explosiveButton.addActionListener(new ActionListener(){
			public void actionPerformed( ActionEvent ae ){
				if (game.toolbar != null)
					game.toolbar.highlight(new Explosive());
				}
			}
		);
		barricadeButton.addActionListener(new ActionListener(){
			public void actionPerformed( ActionEvent ae ){
				if (game.toolbar != null)	
					game.toolbar.highlight(new Barricade());
				}
			}
		);
		/* adding MouseListener */
		/* adding buttons to button component*/
		buttonComponent.add(giveupButton);
		buttonComponent.add(parachuteButton);
		buttonComponent.add(erectorButton);
		buttonComponent.add(explosiveButton);
		buttonComponent.add(drillButton);
		buttonComponent.add(barricadeButton);
		/* setting up the JFrame and adding components */
		this.setLayout(new BorderLayout());
		this.add(mapComponent);
		this.add(buttonComponent, BorderLayout.PAGE_END);
		mapComponent.addMouseListener(mouseControl);
		this.setSize(WIDTH, HEIGHT);
		setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
		this.setVisible(true);
		this.buttonComponent.setVisible(true);
		this.repaint();
	}
	public void paint(Graphics g) {
		//g.setColor(Color.white);
		//g.fillRect(20, 40, size*20, size*20);
		int[] position;
		position = new int[2];
		for (int i=1; i <= size; i++) {
			position[0] = i;
			for (int j=1; j <= size; j++) {
				position[1] = j;
				if (field.check(position) == null || !(field.check(position) instanceof Stone)) {
					int[] blankPos;
					blankPos = new int[2];
					blankPos = this.fieldToView(position);
					g.setColor(Color.white);
					g.fillRect(blankPos[0], blankPos[1], 20, 20);
				}
			}
		}
		for (int i=1; i <= size; i++) {
			position[0] = i;
			for (int j=1; j <= size; j++) {
				position[1] = j;
				if (field.check(position) != null) {
					if (field.check(position) instanceof SetLemming) {
						for (int lemnum = 0; lemnum < ((SetLemming)(field.check(position))).getLemNum(); lemnum++) {
							lemmingAnim.animate(((SetLemming)(field.check(position))).getLem(lemnum), g, phase);
						}
					} else if (field.check(position) instanceof Lemming) {
						lemmingAnim.animate((Lemming)(field.check(position)), g, phase);
					} else if (field.check(position) instanceof Stone) {
						stoneAnim.animate((Stone)(field.check(position)), g);
					} else if (field.check(position) instanceof FinishField) {
						finishAnim.animate((FinishField)(field.check(position)), g);
					} else if (field.check(position) instanceof StartField) {
						startAnim.animate((StartField)(field.check(position)), g);
					}
				} /*else {
>>>>>>> .r187
					int[] blankPos;
					blankPos = new int[2];
					blankPos = this.fieldToView(position);
					//g.setColor(Color.white);
					g.clearRect(blankPos[0], blankPos[1], 20, 20);
				} else {
					if (field.check(position) != null) {
						if (field.check(position) instanceof SetLemming) {
							for (int lemnum = 0; lemnum < ((SetLemming)(field.check(position))).getLemNum(); lemnum++) {
								lemmingAnim.animate(((SetLemming)(field.check(position))).getLem(lemnum), g, phase);
							}
						} else if (field.check(position) instanceof Lemming) {
							lemmingAnim.animate((Lemming)(field.check(position)), g, phase);
						} else if (field.check(position) instanceof Stone) {
							stoneAnim.animate((Stone)(field.check(position)), g);
						} else if (field.check(position) instanceof FinishField) {
							finishAnim.animate((FinishField)(field.check(position)), g);
						} else if (field.check(position) instanceof StartField) {
							startAnim.animate((StartField)(field.check(position)), g);
						}
					} 
				}*/
				if (blown[i][j] == true) {
					int[] blowPos;
					blowPos = new int[2];
					blowPos[0] = i;
					blowPos[1] = j;
					blowAnim.animate(blowPos, g, phase);
				}
			}
		}
		/* set buttons */
		updateButtons();
		this.buttonComponent.repaint();
	}
	/*
	 * from field to x y
	 */
	public int[] fieldToView(int[] fieldPos) {
		int[] viewPos;
		viewPos = new int[2];
		viewPos[0] = 20*fieldPos[0];
		viewPos[1] = (size-fieldPos[1])*20+40;
		return viewPos;
	}
	/*
	 * from x y to field
	 */
	public int[] viewToField(int[] viewPos) {
		int[] fieldPos;
		fieldPos = new int[2];
		fieldPos[0] = viewPos[0]/20;
		fieldPos[1] = size-(viewPos[1]-20)/20;
		return fieldPos;
	}
	public void newRound() {
		for(int i = 1; i <= size; i++) {
			for(int j = 1; j <= size; j++) {
				blown[i][j] = false;
			}
		}
	}
	public void blow(int[] position) {
		blown[position[0]][position[1]] = true;
	}
	/*
	 * updates the View class
	 */
	public void update(Graphics g) {
		
	}
	public void setPhase(int p) {
		phase = p;
	}
}
